package com.ysd.shopcar.controller;

import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ysd.shopcar.common.JSONResult;
import com.ysd.shopcar.param.in.AddProductToCarRequestType;
import com.ysd.shopcar.param.in.ChangeProductNumRequestType;
import com.ysd.shopcar.param.in.SubmitOrderRequestType;
import com.ysd.shopcar.param.out.CarProdcutListResponseType;
import com.ysd.shopcar.param.out.SubmitOrderResponseType;
import com.ysd.shopcar.processor.AddProductToCarProcessor;
import com.ysd.shopcar.processor.ChangeProductNumProcessor;
import com.ysd.shopcar.processor.SubmitOrderProcessor;
import com.ysd.shopcar.service.ShopCarService;

@RestController
@RequestMapping("/shopCar")
public class ShopCarController {
	
	@Autowired
	private ShopCarService shopCarService;
	
	@Value("${server.port}")
	private String port;
	
	@Value("${spring.cloud.client.ip-address}")
	private String ip;
	
	@Resource
	private ChangeProductNumProcessor changeProductNumProcessor;
	@Resource
	private AddProductToCarProcessor addProductToCarProcessor;
	@Resource
	private SubmitOrderProcessor submitOrderProcessor;
	
	
	/**
	 * 根据用户ID查询购物车中商品信息
	 * @param uid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@GetMapping("/{uid}")
	public JSONResult<CarProdcutListResponseType> getCatProductsByUID(@PathVariable Long uid) {
		if(uid == null || uid == 0) {
			return JSONResult.build(-1, "非法入参-"+uid, null);
		}else {
			return shopCarService.getCatProductsByUID(uid);
		}
	}
	/**
	 * 添加/减少购物车商品数量/删除购物车商品
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/changeProductNumByUIDAndPID")
	public JSONResult<ChangeProductNumRequestType> changeProductNumByUIDAndPID(@RequestBody ChangeProductNumRequestType request) {
		return changeProductNumProcessor.process(request);
	}
	
	/**
	 * 添加商品
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addProductToShopCar")
	public JSONResult addProductToShopCar(@RequestBody AddProductToCarRequestType request) {
		if("MS".equals(request.getFlag())) {
			return shopCarService.submitMsOrder(request);
		}else {
			return addProductToCarProcessor.process(request);
		}
	}
	
	/**
	 * test
	 * @param request
	 * @return
	 */
//	@RequestMapping(value = "/test")
//	public UploadFileResponseType test() {
//		UploadFileRequestType request = new UploadFileRequestType();
//		return fileUploadFeginClient.handleFileUploadTest(request);
//	}
	@RequestMapping(value = "/submitOrder")
	public JSONResult<SubmitOrderResponseType> submitOrder(@RequestBody SubmitOrderRequestType request){
		return submitOrderProcessor.process(request);
	}
	

}
